/*
package com.fanjia.demologin.util;

import org.springframework.util.StringUtils;
import sun.rmi.runtime.Log;

*/
/*
 * @author fanjia
 * @className CaptchaUtil
 * @description TODO
 * @date 2021/3/9 16:06
 * <p>
 * 发送登录验证码
 * @param mobile    手机号
 * @param seconds   有效时长(单位: 秒)
 * @return 验证码
 **//*


public class CaptchaUtil {

*/
/*
 * 发送登录验证码
 * @param mobile    手机号
 * @param seconds   有效时长(单位: 秒)
 * @return 验证码
 **//*


    public ResultUtil sendCaptcha(String mobile, Integer seconds){
        Redis redis= RedisManager.getRedis();
        String key = BaseConstant.PHONE_LOGIN_KEY + mobile;
        //默认有效时长60秒
        Integer effTime = 60;
        if (redis.exists(key)){
            return ResultUtil.error("验证码仍在有效期");
        }
        if(StringUtils.isEmpty(mobile)){
            return R.error("参数mobile不能为空");
        }
        if(seconds != null){
            effTime = seconds;
        }
        String captcha = "";

        try{
            //查询短信余额
            Integer smsNum = (Integer) (queryBal().get("data"));
            if(smsNum == 0){
                return R.error("短信余额不足，请联系平台续费！");
            }

            StringBuffer sb = new StringBuffer();
            //生成六位随机数
            captcha = String.valueOf((int)((Math.random() * 9 + 1 ) * 100000));
            String content = "您的登录验证码为: " + captcha + "，有效时长" + effTime/60 + "分钟，请尽快完成登录，如非本人操作请忽略此短信！";
            sb.append("username=" + SmsConstant.SMS_USERNAME)
                    .append("&userpwd=" + SmsConstant.SMS_PASSWORD)
                    .append("&mobiles=" + mobile)
                    .append("&content=" + URLEncoder.encode(SmsConstant.SMS_SIGN + content, "UTF-8"));

            //发送请求
            String resultXml = HttpUtil.sendPost(SmsConstant.SEND_URL, sb.toString());
            AdminSms adminSms = new AdminSms();
            adminSms.setMobile(mobile);
            adminSms.setContent(content);
            adminSms = parseXml(resultXml, adminSms);
            if(adminSms.getErrorcode() == 1){
                //存储redis缓存----根据前端传递的有效时长缓存验证码
                redis.setex(key, effTime-1, captcha);
                //存储短信数据
                adminSms.setSendTime(new Date());
                adminSms.save();
            }
        } catch (Exception e) {
            Log.getLog(getClass()).error("发送短信验证码异常: ", e);
        }

        return R.ok().put("data", captcha);
    }


}
*/
